//
//  WBMiddleView.m
//  WeiBoOC
//
//  Created by 韩菲菲 on 16/8/16.
//  Copyright © 2016年 韩菲菲. All rights reserved.
//

#import "WBMiddleView.h"
#import "UILabel+WBLabel.h"
#import <Masonry.h>
#import "WBStatusViewModel.h"
#import "WBStatusPictureView.h"

@interface WBMiddleView ()

//label显示正文的控件
@property (nonatomic , strong) UILabel *contentLabel;

//显示配图的控件
@property (nonatomic , strong) WBStatusPictureView *pictureView;

//记录约束的属性
@property (nonatomic , strong) MASConstraint *middleViewBottomConstraints;

@end

@implementation WBMiddleView

- (instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame])
    {
        [self setupUI];
    
    }
    
    return self;
}

#pragma mark - 重写赋值接口的set方法 给控件赋值
- (void)setStatusViewModel:(WBStatusViewModel *)statusViewModel
{
    _statusViewModel = statusViewModel;
    
    //1 给label赋值
    self.contentLabel.text = statusViewModel.statusModel.retweeted_status.text;
    
    //卸载约束
    [self.middleViewBottomConstraints uninstall];
    //判断有没有配图
    if(statusViewModel.statusModel.retweeted_status.pic_urls.count > 0)
    {
        //有配图
        //赋值
        //给配图控件赋值
        self.pictureView.pictInfos = statusViewModel.statusModel.retweeted_status.pic_urls;
        //更新约束
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
           self.middleViewBottomConstraints = make.bottom.mas_equalTo(self.pictureView).offset(10);
        }];
        //显示配图
        self.pictureView.hidden = NO;
    
    }else
    {
        //没有配图
        //更新约束
        [self mas_makeConstraints:^(MASConstraintMaker *make) {
            //当前view的底部和label的底部距离差10
            self.middleViewBottomConstraints = make.bottom.mas_equalTo(self.contentLabel).offset(10);
        }];
        //隐藏配图
        self.pictureView.hidden = YES;
    }
}

#pragma mark -懒加载正文label
- (UILabel *)contentLabel
{
    if(!_contentLabel)
    {
        //1  创建控件
        _contentLabel = [[UILabel alloc] initWithFont:15 andTextColor:[UIColor darkGrayColor] andMaxWidth:kScreenW - 20];
        
    }
    
    return _contentLabel;
}

- (WBStatusPictureView *)pictureView
{
    if(!_pictureView)
    {
    
        _pictureView = [[WBStatusPictureView alloc] init];
    }

    return _pictureView;
}


#pragma mark - 设置UI
- (void)setupUI
{
    //设置随机颜色
    self.backgroundColor = [UIColor colorWithRed:237/255.0 green:237/255.0 blue:237/255.0 alpha:1];
    
    //添加控件
    [self addSubview:self.contentLabel];
    [self addSubview:self.pictureView];
    
    //设置约束
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.leading.offset(10);
        
    }];
    
    [self.pictureView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.contentLabel.mas_bottom).mas_offset(10);
        make.leading.mas_equalTo(self).mas_offset((kScreenW - 250) * 0.5);
        
    }];
    
    //设置当前view的约束
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
       //当前view的底部和label的底部距离差10
       self.middleViewBottomConstraints = make.bottom.mas_equalTo(self.contentLabel).offset(10);
        
        
    }];
    
    
}





@end
